package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.page.TableDataApi;
import com.ruoyi.common.enums.dict.ForeclosedHouseStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.Datacenter;
import com.ruoyi.yipaiju.domain.DiscountHouse;
import com.ruoyi.yipaiju.domain.ForeclosedHouse;
import com.ruoyi.yipaiju.domain.House;
import com.ruoyi.yipaiju.service.IDatacenterService;
import com.ruoyi.yipaiju.service.IDiscountHouseService;
import com.ruoyi.yipaiju.service.IForeclosedHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 房源
 */
@RestController
@RequestMapping("/houses")
public class HousesController extends BaseController {

    /**
     * type==1?'优选':'折扣'
     * 优选房源  == 法拍房
     */

    @Autowired
    private IForeclosedHouseService foreclosedHouseService;
    @Autowired
    private IDiscountHouseService discountHouseService;
    @Autowired
    private IDatacenterService datacenterService;

    /**
     * 首页房源列表
     * @param house
     * @return
     */
    @Anonymous
    @GetMapping("")
    public ApiResult list(House house) {
        // TODO 添加按位置显示排序
        // TODO 好像是把折扣和优选都查出来
        // 看看提交个类型吧。 根据类型查询不同条件
        /**
         *  TODO 热门房源好像是看点击率，推荐房源是根据定位
         */
        startPageApi();
        List<DiscountHouse> discountHouses = discountHouseService.selectDiscountHouseList(Builder.of(DiscountHouse::new)
                .with(DiscountHouse::setLongitude, house.getLongitude())
                .with(DiscountHouse::setLatitude, house.getLatitude())
                .with(DiscountHouse::setOrderings, house.getOrderings())
                .with(DiscountHouse::setCity, house.getCity())
                .with(DiscountHouse::setStatus, 2)
                .build());

        startPageApi();
        List<ForeclosedHouse> foreclosedHouses = foreclosedHouseService.selectForeclosedHouseList(Builder.of(ForeclosedHouse::new)
                .with(ForeclosedHouse::setLongitude, house.getLongitude())
                .with(ForeclosedHouse::setLatitude, house.getLatitude())
                .with(ForeclosedHouse::setOrderings, house.getOrderings())
                .with(ForeclosedHouse::setCity, house.getCity())
                .with(ForeclosedHouse::setStartTime, new Date())
                .with(ForeclosedHouse::setPriceYuan1, 1L)
                .with(ForeclosedHouse::setStatus, ForeclosedHouseStatus.TODO.getValue())
                .build());


        List<House> houses = new ArrayList<>();
        // 优选
        for (ForeclosedHouse foreclosedHouse : foreclosedHouses) {
            House house1 = new House();
            BeanUtils.copyProperties(foreclosedHouse, house1);
            house1.setType(1);
            house1.setForeclosedHouseId(foreclosedHouse.getId());
            houses.add(house1);
        }
        // 折扣
        for (DiscountHouse discountHouse : discountHouses) {
            House house1 = new House();
            BeanUtils.copyProperties(discountHouse, house1);
            house1.setStatus(String.valueOf(discountHouse.getStatus()));
            house1.setStage(String.valueOf(discountHouse.getStage()));
            house1.setType(2);
            house1.setDiscountHouseId(discountHouse.getId());
            houses.add(house1);
        }

        return ApiResult.success(houses);
    }


    /**
     * 今日成交
     * @param datacenter
     * @return
     */
    @GetMapping("deal_today")
    public TableDataApi deal_today(Datacenter datacenter) {
        datacenter.setStatus("已成交");
        return queryHouses(datacenter);
    }

    /**
     * 今日新增
     * @param datacenter
     * @return
     */
    @GetMapping("added_today")
    public TableDataApi added_today(Datacenter datacenter) {
        return queryHouses(datacenter);
    }

    /**
     * 今日流拍
     * @param datacenter
     * @return
     */
    @GetMapping("failed_today")
    public TableDataApi failed_today(Datacenter datacenter) {
        datacenter.setStatus("已流拍");
        return queryHouses(datacenter);
    }

    public TableDataApi queryHouses(Datacenter datacenter){
        startPageApi();
        List<ForeclosedHouse> foreclosedHouses = foreclosedHouseService.selectForeclosedHouseList(Builder.of(ForeclosedHouse::new)
                .with(ForeclosedHouse::setProvince, datacenter.getProvince())
                .with(ForeclosedHouse::setCity, datacenter.getCity())
                .with(ForeclosedHouse::setDistrict, datacenter.getDistrict())
                .with(ForeclosedHouse::setDate, datacenter.getDate())
                .with(ForeclosedHouse::setStatus, datacenter.getStatus())
                .with(ForeclosedHouse::setStatusArr, datacenter.getStatusArr())
                .build());
        TableDataApi tableApi = getTableApi(foreclosedHouses);
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> resultMap = (Map<String, Object>) tableApi.getData();
        resultMap.forEach((key, value) -> { dataMap.put(key, value); });
        // 月新增数量
        String beginTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.addDays(new Date(), -30));
        datacenter.setDate(beginTime);
        dataMap.put("totalThatMonth", datacenterService.dayAddedCountForeclosed(datacenter));
        tableApi.setData(dataMap);
        return tableApi;
    }

    /**
     * 今日预约
     * @param datacenter
     * @return
     */
    @GetMapping("no_participant")
    public TableDataApi no_participant(Datacenter datacenter) {
        startPageApi();
        List<ForeclosedHouse> foreclosedHouses = datacenterService.dayAddedBookingList(datacenter);
        return getTableApi(foreclosedHouses);
    }

}
